package com.lingjtx.common.other.helper;

import com.lingjtx.common.core.constant.Const;
import com.lingjtx.common.core.util.BeanUtil;
import org.thymeleaf.context.Context;

import java.util.Objects;

public enum HtmlTemplate {
    ACTIVE_EMAIL("active_email"), // 激活邮箱

    CODE_MESSAGE("code_message"), // 验证码
    ;


    private String path;

    private TemplateEntity<Object> template;

    HtmlTemplate(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public TemplateEntity<Object> getTemplate() {
        return template;
    }

    public void setTemplate(TemplateEntity<Object> template) {
        this.template = template;
    }

    @Override
    public String toString() {
        return this.toHtml();
    }

    private String toHtml() {
        Context context = new Context();
        // html中参数与data 数据源绑定
        if (this.getTemplate() == null) {
            return null;
        }
        context.setVariables(BeanUtil.covertMap(this.getTemplate().getData()));
        context.setVariable("code", getTemplate().getCode());
        context.setVariable("msg", getTemplate().getMsg());
        context.setVariable("serverName", Const.SERVER_NAME);
        context.setVariable("serverLogo", Const.SERVER_LOGO);
        context.setVariable("serverIcon", Const.SERVER_ICON);
        return this.getTemplate().getTemplateEngine().process(this.getPath(), context);
    }

    public byte[] getBytes() {
        return Objects.requireNonNull(toHtml()).getBytes();
    }
}